package org.eenie.core.base

import android.content.Context
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat

abstract class BaseActivity : AppCompatActivity() {

    protected open var mWinMode = WinMode.IMMERSE

    lateinit var mContext: Context

    override fun onCreate(savedInstanceState: Bundle?) {
        mContext = this
        setWinMode(mWinMode)
        initData(savedInstanceState)
        super.onCreate(savedInstanceState)
        if (getLayoutRes() > 0) {
            setContentView(getLayoutRes())
            initUI()
        }
    }

    open fun initData(savedInstanceState: Bundle?) {

    }

    abstract fun getLayoutRes(): Int

    abstract fun initUI()


    fun setWinMode(mode: WinMode) {
        mWinMode = mode
        val ctrl = WindowCompat.getInsetsController(window, findViewById(android.R.id.content))
        when (mode) {
            WinMode.IMMERSE -> {
                ctrl?.show(WindowInsetsCompat.Type.systemBars())
                WindowCompat.setDecorFitsSystemWindows(window, false)
            }
            WinMode.NORMAL -> {
                ctrl?.show(WindowInsetsCompat.Type.systemBars())
                WindowCompat.setDecorFitsSystemWindows(window, true)
            }
            WinMode.FULL -> {
                ctrl?.hide(WindowInsetsCompat.Type.systemBars())
                ctrl?.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
            }
        }
    }


    fun setCutoutMode(mode: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            val lp = window.attributes
            // 仅当缺口区域完全包含在状态栏之中时，才允许窗口延伸到刘海区域显示
//                    lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
            // 永远不允许窗口延伸到刘海区域
//                    lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER
            // 始终允许窗口延伸到屏幕短边上的刘海区域
//            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
            lp.layoutInDisplayCutoutMode = mode
            window.attributes = lp
        }
    }


}